package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constants.user.AdminConstans;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.wemedia.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service

public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WmUserFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        //判断参数是否为空
        if (dto == null) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        QueryWrapper<ApUserRealname> wrapper = new QueryWrapper<>();
        if (dto.getStatus() != null) {
            wrapper.lambda().eq(ApUserRealname::getStatus, dto.getStatus());
        }
        Page pageParam = new Page(dto.getPage(), dto.getSize());
        IPage page = page(pageParam, wrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setCode(0);
        responseResult.setData(page.getRecords());
        return responseResult;

    }

    @Override
    @Transactional
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto) {
        //检查参数
        if (dto == null || dto.getId() == null || dto.getStatus() == null || (!dto.getStatus().equals(AdminConstans.FAIL_AUTH) && !dto.getStatus().equals(AdminConstans.PASS_AUTH))) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        apUserRealname.setUpdatedTime(new Date());
        if (dto.getMsg() != null) {
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);
        //认证通过添加自媒体账号和作者账号
        if (dto.getStatus().equals(AdminConstans.PASS_AUTH)) {
            ApUserRealname rzinfo = getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(rzinfo.getUserId());
            WmUser wm = wemediaFeign.findByName(apUser.getName());
            if (wm == null || wm.getId() == null) {
                WmUser wmUser = new WmUser();
                //设置apuserid
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setStatus(AdminConstans.PASS_AUTH.intValue());
                wmUser.setPhone(apUser.getPhone());

                //这一块报错了!!!!!!!!!!
                wemediaFeign.save(wmUser);
                ApAuthor author = articleFeign.findByUserId(apUser.getId());
                if (author == null) {
                    ApAuthor apAuthor = new ApAuthor();
                    apAuthor.setName(apUser.getName());
                    apAuthor.setType(2);
                    apAuthor.setCreatedTime(new Date());
                    apAuthor.setUserId(apUser.getId());
                    WmUser wmUser1 = wemediaFeign.findByName(apUser.getName());
                    apAuthor.setWmUserId(wmUser1.getId());
                    articleFeign.save(apAuthor);
                    apUser.setFlag(Short.valueOf("1"));
                    apUserMapper.updateById(apUser);
                    ApAuthor author1 = articleFeign.findByUserId(apUser.getId());
                    wmUser1.setApAuthorId(author1.getId());
                    wemediaFeign.updateById(wmUser1);
                // int a=1/0;
                }

            }

            return ResponseResult.okResult(0, "认证通过");


        }
        return ResponseResult.okResult(0, "拒绝通过");
    }
}
